package com.slt.android.customview.pichandle;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.slt.android.customview.R;

/**
 * PorterDuffXfermode
 * 刮刮卡效果
 * Created by dp on 2018/1/9.
 */

public class PoterView extends View {

    Bitmap mBitmap , mBgBitmap , mFgBitmap;
    Bitmap mOut;
    Paint paint;

    Path mPath;

    Canvas mCanvas;
    public PoterView(Context context) {
        super(context);
        init(context);
    }

    public PoterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PoterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
//        mBitmap = BitmapFactory.decodeResource(getResources() , R.mipmap.dlrb);
//        mOut = Bitmap.createBitmap(mBitmap.getWidth() , mBitmap.getHeight() , Bitmap.Config.ARGB_8888);
//        Canvas canvas = new Canvas(mOut);
//        paint = new Paint();
//        paint.setAntiAlias(true);
//        RectF rectF = new RectF(0 , 0 ,mBitmap.getWidth() , mBitmap.getHeight());
//        canvas.drawRoundRect(rectF , 20 , 20 , paint);
//        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
//        canvas.drawBitmap(mBitmap , 0 , 0 , paint);

//        mBitmap = BitmapFactory.decodeResource(getResources(),
//                R.mipmap.dlrb);
//        mOut = Bitmap.createBitmap(mBitmap.getWidth(),
//                mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
//        Canvas canvas = new Canvas(mOut);
//        mPaint = new Paint();
//        mPaint.setAntiAlias(true);
          //这个方法4.4上没有必须先new Recf
//        canvas.drawRoundRect(0, 0,
//                mBitmap.getWidth(),
//                mBitmap.getHeight(), 80, 80, mPaint);
//        mPaint.setXfermode(new PorterDuffXfermode(
//                PorterDuff.Mode.SRC_IN));
//        canvas.drawBitmap(mBitmap, 0, 0, mPaint);


        //刮刮卡初始化
        //在使用PorterDuffXfermode时需要注意关闭硬件加速，因为有些模式不支持
        paint = new Paint();
        paint.setAlpha(20);//0 全透明----255 不透明
//        paint.setColor(Color.RED);
        paint.setXfermode(
                new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(50);
        paint.setStrokeCap(Paint.Cap.ROUND);

        mPath = new Path();
        mBgBitmap = BitmapFactory.decodeResource(getResources() , R.mipmap.dlrb);
        mFgBitmap = Bitmap.createBitmap(mBgBitmap.getWidth() , mBgBitmap.getHeight() , Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mFgBitmap);
        mCanvas.drawColor(Color.GRAY);

    }

    @Override
    protected void onDraw(Canvas canvas) {
//        canvas.drawBitmap(mOut, 0, 0, null);
        canvas.drawBitmap(mBgBitmap , 0 , 0 , null);
        canvas.drawBitmap(mFgBitmap , 0 , 0 , null);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mPath.reset();
                mPath.moveTo(event.getX() , event.getY());
                break;

            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(event.getX() , event.getY());
                break;
            case MotionEvent.ACTION_UP:

                break;
            default:
                break;
        }
        mCanvas.drawPath(mPath, paint);
        invalidate();
        return true;
    }
}
